package com.gwq.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwq.dto.SysMenuDto;
import com.gwq.entity.SysMenu;
import com.gwq.entity.SysUser;
import com.gwq.mapper.SysMenuMapper;
import com.gwq.mapper.SysUserMapper;
import com.gwq.service.SysMenuService;
import com.gwq.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gwq
 * @since 2022-06-07
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        String userName = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserService.getByUserName(userName);
        List<String> menuIds = sysUserMapper.getNavMenuIds(sysUser.getId());
        List<SysMenuDto> sysMenuDtos = new ArrayList<>();
        if(menuIds.size()>0){
            List<SysMenu> sysMenus = (List<SysMenu>) this.listByIds(menuIds);
            // 转树状结构
            if(sysMenus.size()!=0) {
                List<SysMenu> menuTree = buildTreeMenu(sysMenus);
                sysMenuDtos = convert(menuTree);
            }
        }
        // 实体转dto
        return sysMenuDtos;
    }

    @Override
    public List<SysMenu> tree() {
        // 获取所有菜单信息
        List<SysMenu> orderNum = this.list(new QueryWrapper<SysMenu>().orderByAsc("order_num"));

        // 转
        List<SysMenu> menus = buildTreeMenu(orderNum);

        return menus;
    }

    private List<SysMenuDto> convert(List<SysMenu> menuTree) {

        List<SysMenuDto> menuDtos = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuDto sysMenuDto = new SysMenuDto();
            sysMenuDto.setId(m.getId());
            sysMenuDto.setName(m.getPerms());
            sysMenuDto.setTitle(m.getName());
            sysMenuDto.setIcon(m.getIcon());
            sysMenuDto.setComponent(m.getComponent());
            sysMenuDto.setPath(m.getPath());

            if(m.getChildren().size() > 0){
                sysMenuDto.setChildren(convert(m.getChildren()));
            }
            menuDtos.add(sysMenuDto);
        });
        return menuDtos;
    }

    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {

        List<SysMenu> finalMenus = new ArrayList<>();

        // 先各自寻找到各自的孩子
        for (SysMenu menu : menus) {

            for (SysMenu e : menus) {
                if (menu.getId() .equals(e.getParentId())) {
                    menu.getChildren().add(e);
                }
            }

            // 提取出父节点
            if (menu.getParentId().equalsIgnoreCase("0")) {
                finalMenus.add(menu);
            }
        }

        System.out.println(JSONUtil.toJsonStr(finalMenus));
        return finalMenus;
    }
}
